﻿package com.tvein.sanguo.swc.character
{
	//flash
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class CharacterShell extends MovieClip
	{
		public static const NAME:String = "CharacterShell";

		private var gameConfig:Array;
		
		var Stamina:Shape = new Shape();
		var Energy:Shape = new Shape();

		public function CharacterShell(configArray:Array)
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);

			gameConfig = configArray;
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
            this.addChild(Stamina);
            this.addChild(Energy);
		}
		
		public function loadHead(roleID:int):void
		{
			this.buttonMode = true;
			this.mouseEnabled = true;
			this.addEventListener(MouseEvent.CLICK, onClick);
			
			var faceloader:Loader = new Loader();
			faceloader.load(new URLRequest(gameConfig["srcServer"] + "/SanGuo/role/face/" + roleID + ".png"));
			faceloader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onFaceLoad);
		}
		
		public function SetStamina(num:int):void
		{
			this.removeChild(Stamina);
			
			if (num > 100)
			{
				num = 100;
			}
			
			if (num < 0)
			{
				num = 0;
			}
			
			Stamina = new Shape();
			Stamina.graphics.beginFill(0xFF0000);
            Stamina.graphics.drawRect(9, 93, 13, -60 * (num / 100));
            Stamina.graphics.endFill();
			this.addChild(Stamina);
		}
		
		public function SetEnergy(num:int):void
		{
			this.removeChild(Energy);
			
			if (num > 100)
			{
				num = 100;
			}
			
			if (num < 0)
			{
				num = 0;
			}
			
			Energy = new Shape();
            Energy.graphics.beginFill(0x0000FF);
            Energy.graphics.drawRect(94, 93, 13, -60 * (num / 100));
            Energy.graphics.endFill();
			this.addChild(Energy);
		}
		
		private function _onFaceLoad(evt:Event):void
		{
			evt.target.removeEventListener(Event.COMPLETE, _onFaceLoad);
			
			evt.target.content.scaleX = -1;
			evt.target.content.x = 90;
			evt.target.content.y = 23;
			
			this.addChild(evt.target.content);
			this.visible = true;
			
			this.x = 10;
			this.y = 40;
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			this.removeEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var soundEffect:MP3Click = new MP3Click();
			soundEffect.playback(gameConfig["effectVol"] * .01);
			
			dispatchEvent(new SwcEvent(NAME, ["-roleDetail"]));
		}
	}
}